# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from lib import libtime,libpage,libmd5
from lib.app import libtype
from decorator.auth import admin_login_required

from web.models.admin.user.repairman import Repairman

@route(r'/admin/user/repairman/?', name='')
class IndexHandler(BaseHandler):
    @admin_login_required
    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        offset = self.get_argument('offset', default=self.offset)
        school_id = self.get_argument('schoolId', default=self.school_id)
        start = libpage.start(cur_page, offset)
        result = yield Repairman().list(school_id, start, offset)
        type_list = libtype.get_type_dict()

        page_info={'total':result[0], 'page':int(cur_page)}
        self.render("admin/user/repaireman/list.html",page_info=page_info, man_list=result[1], type_list=type_list)


@route(r'/admin/user/repairman/new/?')
class NewHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        type_list = libtype.get_type_dict()
        self.render("admin/user/repaireman/new.html",type_list=type_list)

    @asynchronous
    @gen.coroutine
    def post(self):
        man_name = self.get_argument('manName', default='')
        username = self.get_argument('username', default='')
        password = self.get_argument('password', default='')
        tel = self.get_argument('tel', default='')
        work_id = self.get_argument('workId', default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)
        password = libmd5.password(password)
        result = yield Repairman().new(man_name, username, password, tel, work_id, school_id)

        self.redirect('/admin/user/repairman')

@route(r'/admin/user/repairman/edit/?')
class EditHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        man_id = self.get_argument('id', default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)
        type_list = libtype.get_type_dict()
        man_info = {}
        login_info = {}
        if man_id == 0:
            self.write_error(404)
        else:
            result = yield Repairman().edit_info(school_id, man_id)
            print school_id, man_id
            print result
            if len(result) > 0:
                man_info = result[0]
                login_info = result[1]
            else:
                self.write_error(500)

        self.render("admin/user/repaireman/edit.html",type_list=type_list, man_info=man_info, login_info=login_info)

    @asynchronous
    @gen.coroutine
    def post(self):
        man_id = self.get_argument('manId', default='')
        man_name = self.get_argument('manName', default='')
        username = self.get_argument('username', default='')
        password = self.get_argument('password', default='')
        tel = self.get_argument('tel', default='')
        work_id = self.get_argument('workId', default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)
        # password = libmd5.password(password)
        result = yield Repairman().edit(man_id, man_name, username, password, tel, work_id, school_id)

        self.redirect('/admin/user/repairman')

@route(r'/admin/user/repairman/del/?')
class DelHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        man_id = self.get_argument('id',default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)
        info_list = []
        if man_id == 0:
            self.write_error(404)
        else:
            result = yield Repairman().delete(man_id,school_id)
        self.redirect('/admin/user/repairman')


@route(r'/admin/user/repairman/search/?')
class SearchHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        man_name = self.get_argument('manName',default='')
        tel = self.get_argument('tel',default='')
        work_id = self.get_argument('workType',default='')
        school_id = self.get_argument('schoolId', default=self.school_id)
        cur_page = self.get_argument('page', default=1)
        offset = self.get_argument('offset', default=self.offset)
        start = libpage.start(cur_page, offset)

        result = yield Repairman().search(man_name,tel,work_id,school_id,start,offset)
        man_list = []
        if len(result) > 0:
            page_info = {'total':result[0], 'page':int(cur_page)}
            man_list = result[1]
        else:
            page_info = {'total':0, 'page':0}
        ser_list = [man_name, tel, work_id]

        type_list = libtype.get_type_dict()
        self.render("admin/user/repaireman/list.html",page_info=page_info, type_list=type_list, man_list=man_list, ser_list=ser_list)